package org.jeecg.modules.mall.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import org.jeecg.annotation.ApiLogin;
import org.jeecg.com.util.ThirdSessionHolder;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.TenantContextHolder;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.config.ApiVersion;
import org.jeecg.config.ApiVersionConstant;
import org.jeecg.config.WebConfig;
import org.jeecg.modules.mall.entity.MallContact;
import org.jeecg.modules.mall.service.IMallContactService;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: mall_contact
 * @Author: jeecg-boot
 * @Date:   2023-04-26
 * @Version: V1.0
 */
@Api(tags="商城联系人")
@RestController
@RequestMapping("/mall-api/contact")
@Slf4j
public class MallContactController extends WebConfig {
	@Resource
	private IMallContactService mallContactService;

	/**
	 * 商城联系人分页列表查询
	 *
	 * @param mallContact
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiLogin
	@ApiVersion(group = ApiVersionConstant.FAP_MALLAPI101)
	@ApiOperation(value="商城联系人分页列表查询", notes="商城联系人分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<MallContact>> queryPageList(MallContact mallContact,
													@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
													@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
													HttpServletRequest req) {
		LambdaQueryWrapper<MallContact> queryWrapper = QueryGenerator.initQueryWrapper(mallContact, req.getParameterMap()).lambda();
		String userId = ThirdSessionHolder.getUserId();
		queryWrapper.eq(MallContact::getUserId,userId);
		Page<MallContact> page = new Page<MallContact>(pageNo, pageSize);
		IPage<MallContact> pageList = mallContactService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加商城联系人
	 *
	 * @param mallContact
	 * @return
	 */
	@AutoLog(value = "添加商城联系人")
	@ApiOperation(value="添加商城联系人", notes="添加商城联系人")
	@ApiLogin
	@ApiVersion(group = ApiVersionConstant.FAP_MALLAPI101)
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody MallContact mallContact) {
		String tenantId = TenantContextHolder.getTenantId();
		String userId = ThirdSessionHolder.getUserId();
		mallContact.setUserId(userId);
		mallContact.setTenantId(tenantId);
		mallContactService.save(mallContact);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑商城联系人
	 *
	 * @param mallContact
	 * @return
	 */
	@AutoLog(value = "编辑商城联系人")
	@ApiOperation(value="编辑商城联系人", notes="编辑商城联系人")
	@ApiLogin
	@ApiVersion(group = ApiVersionConstant.FAP_MALLAPI101)
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody MallContact mallContact) {
		mallContactService.updateById(mallContact);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除商城联系人
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "通过id删除商城联系人")
	@ApiOperation(value="通过id删除商城联系人", notes="通过id删除商城联系人")
	@ApiLogin
	@ApiVersion(group = ApiVersionConstant.FAP_MALLAPI101)
	@PostMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		mallContactService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 通过id查询商城联系人
	 *
	 * @param id
	 * @return
	 */
	@ApiLogin
	@ApiVersion(group = ApiVersionConstant.FAP_MALLAPI101)
	@ApiOperation(value="通过id查询商城联系人", notes="通过id查询商城联系人")
	@GetMapping(value = "/queryById")
	public Result<MallContact> queryById(@RequestParam(name="id",required=true) String id) {
		MallContact mallContact = mallContactService.getById(id);
		if(mallContact==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(mallContact);
	}
}
